package Siebel::Code::Analyzer::DAO::SQLite;

use warnings;
use strict;
use Moose;

extends 'Siebel::Code::Analyzer::DAO';

override '_build__dbh' => sub {

    my $self = shift;

    return DBI->connect( $self->data_source(), '', '' );

};

override 'get_runtime_query' => sub {

    my $self = shift;

    # this query is for runtime BS
    my $sql = <<BLOCK;
SELECT B.NAME,
  B.DISPLAY_NAME,
  A.INACTIVE_FLG,
  B.CLASS_NAME,
  B.SVR_COMPAT_FLG,
  B.COMMENTS,
  A.NAME   AS METHOD_NAME,
  A.SCRIPT AS METHOD_CODE
FROM S_RT_SVC_SCRPT A,
     S_RT_SVC B
WHERE a.RT_SVC_ID = B.ROW_ID
AND A.PROG_LANG   = 'JS'
BLOCK

    return $sql;

};

override 'get_repo_queries' => sub {

    my $self = shift;

    my $in_clause = super();

    my $end;

    if ( $self->skip_inactive() ) {

        $end = <<BLOCK
AND a.inactive_flg = ?
AND b.inactive_flg = ?
AND a.last_upd       > ?;
BLOCK

    }
    else {

        $end = 'AND a.last_upd > ?;';

    }

    my @queries;

    my $sql = <<BLOCK;
SELECT
  c.name        AS repository_name,
  'Application' AS obj_type ,
  b.name        AS application_name,
  a.name        AS script_name,
  a.last_upd,
  a.script
FROM
  S_APPL_SCRIPT a,
  S_APPLICATION b,
  S_REPOSITORY  c
WHERE
  c.row_id           = a.repository_id
AND a.application_id = b.row_id
AND a.repository_id  = b.repository_id
AND c.name           IN ($in_clause) 
$end
BLOCK

    push( @queries, $sql );

    $sql = <<BLOCK;
SELECT
  c.name   AS repository_name,
  'Applet' AS obj_type ,
  b.name   AS applet_name,
  a.name   AS script_name,
  a.last_upd,
  a.script
FROM
  S_APPL_WEBSCRPT a,
  S_APPLET b,
  S_REPOSITORY c
WHERE
  c.row_id          = a.repository_id
AND a.applet_id     = b.row_id
AND a.repository_id = b.repository_id
AND c.name          IN ($in_clause) 
$end
BLOCK

    push( @queries, $sql );

    $sql = <<BLOCK;
SELECT
  c.name               AS repository_name,
  'Business Component' AS obj_type ,
  b.name               AS bc_name,
  a.name               AS script_name,
  a.last_upd,
  a.script
FROM
  S_BUSCOMP_SCRIPT a,
  S_BUSCOMP b,
  S_REPOSITORY c
WHERE
  c.row_id          = a.repository_id
AND a.buscomp_id    = b.row_id
AND a.repository_id = b.repository_id
AND c.name IN ($in_clause) 
$end
BLOCK

    push( @queries, $sql );

    $sql = <<BLOCK;
SELECT
  c.name             AS repository_name,
  'Business Service' AS obj_type ,
  b.name             AS service_name,
  a.name             AS script_name,
  a.last_upd,
  a.script
FROM
  S_SERVICE_SCRPT a,
  S_SERVICE b,
  S_REPOSITORY c
WHERE
  c.row_id          = a.repository_id
AND a.service_id    = b.row_id
AND a.repository_id = b.repository_id
AND c.name            in ($in_clause) 
$end
BLOCK

    push( @queries, $sql );

    return $self->_bind_params( \@queries );

};

=pod

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.org<E<gt>

This project is based on the code (SiebelOVAnalyzer) provided in the article "Memory Leaks in 
Siebel" (http://siebelunleashed.com/memory-leaks-siebel/) from Timur Vafin. 

This file is part of Siebel Code Analyzer.

Siebel Code Analyzer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Siebel Code Analyzer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Siebel Monitoring Tools.  If not, see <http://www.gnu.org/licenses/>.

=cut

__PACKAGE__->meta->make_immutable;

1;
